package yt.kpool.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.zxing.WriterException;
import com.yzq.zxinglibrary.encode.CodeCreator;
import java.util.List;
import yt.kpool.R;
import yt.kpool.bean.CommentItem;
import yt.kpool.bean.RecommendCustomersBean;
import yt.kpool.util.SaveImageUtils;
import yt.kpool.util.ToastUtil;


public class RecommendCustomersAdapter extends ExpandableRecyclerAdapter<CommentItem> {
    public static final int TYPE_PERSON = 1001;
    private Activity context;
    private LRecyclerView recyclerView;
    public RecommendCustomersAdapter(Context context, LRecyclerView recyclerView) {
        super(context);
        this.recyclerView = recyclerView;
        this.context=(Activity)context;
        //setItems(getSampleItems());
    }

    /*public static class CommentItem extends ExpandableRecyclerAdapter.ListItem {
        public String Text;

        public CommentItem(String group) {
            super(TYPE_HEADER);
            Text = group;
        }

        public CommentItem(String first, String last) {
            super(TYPE_PERSON);
            Text = first + " " + last;
        }
    }*/

    public class CommentViewHolder extends ExpandableRecyclerAdapter.HeaderViewHolder {
        TextView tv_ProportionName, tv_time;

        public CommentViewHolder(View view, LRecyclerView recyclerView) {
            super(view, (ImageView) view.findViewById(R.id.item_arrow),recyclerView);

            tv_ProportionName = view.findViewById(R.id.tv_ProportionName);
            tv_time =view.findViewById(R.id.tv_time);


        }

        public void bind(int position) {
            super.bind(position);

            tv_ProportionName.setText(visibleItems.get(position).getProportionName());
            tv_time.setText(visibleItems.get(position).getCreatedTime());
        }
    }

    public class CommentChildViewHolder extends ExpandableRecyclerAdapter.ViewHolder {
        TextView tv_Code,tv_copy,tv_save,tv_look;
        ImageView imageView;
        public CommentChildViewHolder(View view) {
            super(view);
            tv_Code=view.findViewById(R.id.tv_Code);
            imageView=view.findViewById(R.id.image);
            tv_copy=view.findViewById(R.id.tv_copy);
            tv_save=view.findViewById(R.id.tv_save);
            tv_look=view.findViewById(R.id.tv_look);
        }

        public void bind(final int position) {
            tv_Code.setText(visibleItems.get(position).getCode());
            final Bitmap bitmap=getImg(visibleItems.get(position).getCode());
            imageView.setImageBitmap(bitmap);
            tv_copy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    copyTextToClipboard(visibleItems.get(position).getCode());
                }
            });
            tv_save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SaveImageUtils.saveImageToGallerys(context,bitmap);
                }
            });
            tv_look.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog(context,R.layout.dialog_sharefloor,visibleItems.get(position).getProportionsDetails());
                }
            });
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEADER:
                //header中的箭头默认隐藏，如有需要，item_arrow设置为visible即可
                return new CommentViewHolder(inflate(R.layout.item_comment, parent), recyclerView);
            case TYPE_PERSON:
            default:
                return new CommentChildViewHolder(inflate(R.layout.item_child_comment, parent));
        }
    }

    @Override
    public void onBindViewHolder(ExpandableRecyclerAdapter.ViewHolder holder, final int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEADER:
                ((CommentViewHolder) holder).bind(position);
                break;
            case TYPE_PERSON:
            default:
                ((CommentChildViewHolder) holder).bind(position);
                break;
        }


    }

    /*
     * contentEtString：字符串内容
     * w：图片的宽
     * h：图片的高
     * logo：不需要logo的话直接传null
     * */

    private Bitmap getImg(String contentEtString) {
        Bitmap logo = BitmapFactory.decodeResource(context.getResources(), R.mipmap.failimage);
        try {
            return CodeCreator.createQRCode(contentEtString, 400, 400, null);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return logo;
    }

    /**
     * 复制
     * @param text
     */
    public void copyTextToClipboard(final String text) {
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ClipboardManager clipboardManager = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("content", text);
                clipboardManager.setPrimaryClip(clipData);
                ToastUtil.showToast(context,"复制成功");
            }
        });
    }

    private void showDialog(Context context, @LayoutRes int resource, List<RecommendCustomersBean.DataBean.ProportionsDetailsBean> list) {
        //设置要显示的view
        View view = View.inflate(context, resource, null);
        LRecyclerView recyleView=view.findViewById(R.id.recyleview);
        RCAdapter rcAdapter=new RCAdapter(context,list);
        LRecyclerViewAdapter mLRecyclerViewAdapter = new LRecyclerViewAdapter(rcAdapter);
        recyleView.setAdapter(mLRecyclerViewAdapter);
        recyleView.setLayoutManager(new LinearLayoutManager(context));
        recyleView.setPullRefreshEnabled(false);

        final Dialog dialog = new Dialog(context, R.style.dialog);
        //此处可按需求为各控件设置属性
        view.findViewById(R.id.dialog_voicegame_share_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog.setContentView(view);
        Window window = dialog.getWindow();
        //设置弹出窗口大小
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //设置显示位置
        window.setGravity(Gravity.BOTTOM);
        //设置动画效果
        window.setWindowAnimations(R.style.AnimBottom);
        dialog.show();
    }

}
